package cn.lupin.message;

import cn.lupin.utils.SftpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class MessageHandler {

    private static final Logger log = LoggerFactory.getLogger(MessageHandler.class);

    private static final String DEFAULT_QUEUES = "send-queue-one";

    // bgm所在目录
    public static final String BGM_PATH = "/root/app/laugh-videos-backend/bgm/";

    @Autowired
    private MessageConverter messageConverter;


    @SuppressWarnings("unchecked")
    @RabbitListener(queues = {DEFAULT_QUEUES})
    public void receiveDirect(Message message) {
        log.info("receiveDirect message: {}", message);
        Map<String, String> msg = (Map<String, String>) messageConverter.fromMessage(message);
        String path = msg.get("path");
        String[] split = path.split("/");
        String fileName = split[split.length - 1];
        String localFilePath = BGM_PATH + fileName;
        SftpUtils.downFile(localFilePath, path);
    }
}
